package com.cx.base.day12_framework.list.arraylist;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

/**
 * @Author: robert.song
 * @Date: Created in 2021/8/16
 */
@SuppressWarnings({"all"})
public class List排序 {
    //    静态方法
    public static void sort(List list) {
        int listSize = list.size();
        for (int i = 0; i < listSize - 1; i++) {
            for (int j = 0; j < listSize - 1 - i; j++) {
                int result = ((Book) list.get(j)).compareTo(((Book) list.get(j + 1)));
                if (result > 0) {
                    Object tb = list.get(j);
                    list.set(j, list.get(j + 1));
                    list.set(j + 1, tb);
                }
            }
        }
    }

    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(new Book("hongloumeng", "caoxueqin", 100));
        list.add(new Book("hongloumeng", "caoxueqin", 100));
        list.add(new Book("hongloumeng", "caoxueqin", 100));
        list.add(new Book("xiyouji", "wuchengen", 10));
        list.add(new Book("xiyouji", "wuchengen", 10));
        list.add(new Book("shuihuzhuan", "shinaian", 9));
        list.add(new Book("shuihuzhuan", "shinaian", 9));
        list.add(new Book("shuihuzhuan", "shinaian", 9));
        list.add(new Book("shuihuzhuan", "shinaian", 9));
        list.add(new Book("sanguoyanyi", "luoguanzhong", 80));
        list.add(new Book("sanguoyanyi", "luoguanzhong", 80));
        list.add(new Book("sanguoyanyi", "luoguanzhong", 80));
        list.add(new Book("sanguoyanyi", "luoguanzhong", 80));
        list.add(new Book("sanguozhi", "chenshou", 20));
        list.add(new Book("sanguozhi", "chenshou", 20));
        list.add(new Book("sanguozhi", "chenshou", 20));
        list.add(new Book("sanguozhi", "chenshou", 20));
        list.add(new Book("sanguozhi", "chenshou", 20));
        list.add(new Book("sanguozhi", "chenshou", 20));

        list.removeIf(new Predicate() {
            @Override
            public boolean test(Object o) {
                Book book = ((Book) o);
                System.out.println("predicate:" + book.toString());
                if (book.getPrice() == 20) {
//                    true就是符合删除条件
                    return true;
                }
                return false;
            }
        });

        list.replaceAll(o -> {
            ((Book) o).setName("unaryOperator");
            return o;
        });
//        上面lamda表达式是指UnaryOperator#apply:Object的表达
//        list.replaceAll(new UnaryOperator() {
//            @Override
//            public Object apply(Object o) {
//                ((Book)o).setName("unaryOperator");
//                return o;
//            }
//        });

        list.forEach(o -> {
            o.toString();
        });

//        排序前遍历
        for (Object o : list) {
            System.out.println("list after predicate:" + ((Book) o).toString());
        }

//        LinkedList
        List list2 = new LinkedList();
        list2.add(new Book());
        list2.addAll(list);

        for (Object o : list2) {
            System.out.println(((Book) o).toString());
        }

        System.out.println(list.get(2));
        System.out.println(list.indexOf(new Book("sanguozhi", "chenshou", 20)));
        System.out.println(list.lastIndexOf(new Book("sanguozhi", "chenshou", 20)));
        System.out.println(list.size());
        System.out.println(list.remove(0));
        System.out.println(list.size());
        System.out.println(list.get(0));
        System.out.println(list.set(0, new Book("xiyouji比斯", "songyahui", 0)));
        System.out.println(list.get(0));
        System.out.println("-----------返回指定范围内的子集合（含前不含后）-----------");
        System.out.println(list.subList(0, 2));
        System.out.println("----------------------");
//        排序结果
        sort(list);
//        排序后遍历
        for (Object o : list) {
            System.out.println(((Book) o).toString());
        }
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Book implements Comparable<Book> {
    private String name;
    private String author;
    private double price;

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", author='" + author + '\'' +
                '}';

    }


    @Override
    public int compareTo(Book o) {
        return this.name.compareTo(o.getName());
    }
}